package cn.edu.ptu.jobfairs.core.utils;

import java.lang.reflect.Field;
import java.util.Collection;

/**
 * Created by Lin Chenxiao on 2020-05-28
 **/
public class ValidUtils {
    public static boolean hasBlank(Object obj, String... skipFields) {
        if (obj == null) return true;
        try {
            Class clazz = obj.getClass();
            while (clazz != null) {
                a: for (Field field : obj.getClass().getDeclaredFields()) {
                    if (skipFields != null && skipFields.length > 0) {
                        for (String name : skipFields) {
                            if (name != null && name.equals(field.getName())) continue a;
                        }
                    }
                    field.setAccessible(true);
                    if (isEmpty(field.get(obj))) return true;
                }
                clazz = clazz.getSuperclass();
            }
        }
        catch (Exception e) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(Object... obj) {
        if (obj == null || obj.length == 0) return true;
        for (Object v : obj) if (v == null || v instanceof String && ((String) v).isEmpty() ||
                v instanceof Collection && ((Collection) v).isEmpty()) return true;
        return false;
    }

    public static boolean intEquals(Integer i, int j) {
        return i != null && i == j;
    }
}
